from __future__ import annotations

from datetime import datetime
from typing import TYPE_CHECKING

from sqlalchemy import Boolean, DateTime, ForeignKey, Integer, JSON, String
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.db.base_class import BaseEntity

if TYPE_CHECKING:
  from .relationship import Relationship
  from .user import User


class DecisionPreset(BaseEntity):
  __tablename__ = "decision_presets"

  user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True)
  relationship_id: Mapped[int | None] = mapped_column(ForeignKey("relationships.id"), index=True)

  title: Mapped[str] = mapped_column(String(180))
  options_json: Mapped[list[str]] = mapped_column(JSON, default=list)
  style: Mapped[str] = mapped_column(String(32), default="wheel")
  is_shared: Mapped[bool] = mapped_column(Boolean, default=False)
  color_seed: Mapped[str | None] = mapped_column(String(32), nullable=True)
  usage_count: Mapped[int] = mapped_column(Integer, default=0)
  last_used_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True)
  last_result: Mapped[str | None] = mapped_column(String(180), nullable=True)

  user: Mapped["User"] = relationship(back_populates="decision_presets", lazy="joined")
  relationship: Mapped["Relationship | None"] = relationship("Relationship", lazy="joined")
